package com.liuhui;

import com.common.collection.Classes;
import com.common.collection.Student;

import java.util.*;

/**
 * date: 2023/3/23 8:49
 *
 * @author 刘辉
 * @Discription
 * @since JDK 1.8
 */
public class StudentDemo {
    public static void main(String[] args) {

        Classes cla = new Classes();
        System.out.println(cla.students());

        filterByAge(cla.students());
        distinctName(cla.students());
        nameTimes(cla.students());
        sortByAge(cla.students());
        addressAndInfo(cla.students());

    }

    static void filterByAge(List<Student> list){
//        System.out.println(list);

        System.out.println("********  1.根据年龄过滤出小于15岁  ******************");
        for (int i=0; i<list.size(); i++){
            Student s = list.get(i);
            if(s.getAge()<15){
                System.out.print(s+" ");
            }
        }
        System.out.println();

       /* for(Student student: list){
            if(student.getAge() < 15){
                System.out.println(student);
            }
        }*/

      /*  list.forEach(student -> {
            if(student.getAge() < 15){
                System.out.println(student);
            }
        });*/

    }

    static void distinctName(List<Student> list) {
        System.out.println("********  2.对学生姓名进行去重并输出  ******************");

        Set<String> set = new HashSet<>();

        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (set.add(s.getName())) { //重复的话返回false
                System.out.print(s.getName()+" ");
            }
        }
        System.out.println();


    }

    static void nameTimes(List<Student> list) {
        System.out.println("********  3，nameTimes：找统计学生姓名出现的次数并输出。  ******************");
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            String name =s.getName();
            Integer count = map.get(name);
            map.put(name, (count == null) ? 1 : count + 1);
        }
        for ( Map.Entry<String, Integer> entry : map.entrySet()) {
            String mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            System.out.println(mapKey + "：" + mapValue);
        }
    }


    static void sortByAge(List<Student> list) {
        System.out.println("********  4，sortByAge：将学生按照年龄排序并输出  ******************");
//        Sort方法传一个比较器，可以进行排序，但是这种排序不能去重
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                int num1 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                int num2 = num1 == 0 ? o1.getAddress().compareTo(o2.getAddress()) : num1;
                return num2;
            }
        });
        for (Student s : list) {
            System.out.print(s.getName() + "... ");
        }
        System.out.println();

    }

    static void addressAndInfo(List<Student> list) {
        System.out.println("********  5，addressAndInfo：获取 地址-学生信息 的关系  ******************");
        Map<String, String> map = new HashMap<>();

        Set<String> set = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            String address=s.getAddress();
            String stuList = s +" ";
            if (set.add(s.getAddress())==false) { //重复的话返回false
                String str = map.get(address)+","+s;
                map.put(address,str);
            }else {
                map.put(address, stuList);
            }
        }

        for ( Map.Entry<String, String> entry : map.entrySet()) {
            String mapKey = entry.getKey();
            String stu = entry.getValue();
            System.out.println(mapKey + "(" + stu+")");
        }
    }

}
